﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CaculateScore
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入分数，用逗号分隔：");
                string score = Console.ReadLine();
                if (string.IsNullOrEmpty(score))
                    continue;
                List<int> list = new List<int>();
                try
                {
                    string[] array = score.Split(new char[] { ',', '，', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    list = Array.ConvertAll(array, new Converter<string, int>(c => Convert.ToInt32(c))).ToList();
                }
                catch (Exception e)
                {
                    Console.WriteLine("出错：" + e.Message);
                    continue;
                }
                CaculateScore(list);
            }

        }

        static void CaculateScore(List<int> list)
        {
            Console.WriteLine(" ");
            int totalCount = list.Count();
            Console.WriteLine(string.Format("您输入总人数为：{0}人。", totalCount));
            Print(list, 0, 49, totalCount);
            Print(list, 50, 59, totalCount);
            Print(list, 60, 69, totalCount);
            Print(list, 70, 79, totalCount);
            Print(list, 80, 89, totalCount);
            Print(list, 90, 100, totalCount);
            int max = list.Max();
            int min = list.Min();
            double avg = list.Average();
            Console.WriteLine(string.Format("最高分数：{0}", max));
            Console.WriteLine(string.Format("最低分数：{0}", min));
            Console.WriteLine(string.Format("全距：{0}", max - min));
            Console.WriteLine(string.Format("平均分：{0}", avg));
            double sd = StandardDeviation(list, avg, totalCount);
            Console.WriteLine(string.Format("标准差：{0}", sd));

        }
        /// <summary>
        /// 标准差
        /// </summary>
        /// <param name="list"></param>
        private static double StandardDeviation(List<int> list, double avg, int totalCount)
        {
            double result = 0;
            foreach (var num in list)
            {
                double temp = (num - avg);
                temp *= temp;
                result += temp;
            }
            return Math.Sqrt(result / totalCount);
        }

        static void Print(List<int> list, int x, int y, double totalCount)
        {
            int peopleCount = list.Count(c => c >= x && c <= y);
            double proportion = (peopleCount / totalCount) * 100;
            string i = string.Format("{0}--{1}分人数：{2}，占总人数比例 {3}%",
                x, y, peopleCount, proportion);
            if (x == 0)
                i = string.Format("少于{0}分人数：{1}，占总人数比例 {2}%",
                    y, peopleCount, proportion);
            else if (y == 100)
                i = string.Format("高于{0}分人数：{1}，占总人数比例 {2}%",
                    x, peopleCount, proportion);
            Console.WriteLine(i);
        }
    }
}
